/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ceair.flight.refund.util;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class GsonUtil {

    private static final Logger LOG = LoggerFactory.getLogger(GsonUtil.class);
    private static final Gson GSON = new Gson();

    public static String toJson(Object source) {
        try {
            String json = GSON.toJson(source);
            return json;
        } catch (Exception e) {
            LOG.error("Convert object {} to json error", source, e);
        }
        return "";
    }

    public static <T> T fromJson(String dest, Class<T> clazz) {
        try {
            T jsonObject = GSON.fromJson(dest, clazz);
            return jsonObject;
        } catch (Exception e) {
            LOG.error("Convert json {} to class {} error", dest, clazz, e);
        }
        return null;
    }

}
